package com.wxzz.elearing.comment.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxzz.elearing.comment.dto.AdminQueAnsEnterpriseDto;
import com.wxzz.elearing.comment.entity.Banned;
import com.wxzz.elearing.comment.entity.EnterpriseQuesAns;
import com.wxzz.elearing.comment.entity.QueAnsAcceptInfo;
import com.wxzz.elearing.comment.entity.QuestionsAnswersList;
import com.wxzz.elearing.comment.mapper.*;
import com.wxzz.elearing.comment.service.IEnterpriseQuesAnsService;
import com.wxzz.elearing.comment.service.openfeign.IEnterQueAnsApiFeign;
import com.wxzz.elearing.comment.vo.EnterpriseQueAnsVo;
import com.wxzz.elearing.comment.vo.QueAnsUserInfoVo;
import com.wxzz.elearning.common.dto.FeignWxUserDto;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2021-05-18
 */
@Service
public class AdminEnterpriseQuesAnsServiceImpl extends ServiceImpl<AdminEnterpriseQuesAnsMapper, EnterpriseQuesAns> implements IEnterpriseQuesAnsService {

    @Autowired
    private IEnterQueAnsApiFeign iEnterQueAnsApiFeign;

    @Resource
    private AdminEnterpriseQuesAnsMapper adminEnterpriseQuesAnsMapper;

    @Resource
    private QuestionsAnswersListMapper questionsAnswersListMapper;

    @Resource
    private QueAnsAcceptInfoMapper queAnsAcceptInfoMapper;

    @Resource
    private CommunityQuestionsAnswersListMapper communityQuestionsAnswersListMapper;
    public ReturnVO<JgPageVo<List<EnterpriseQueAnsVo>>> getEnterpriseQueAnsList(AdminQueAnsEnterpriseDto adminQueAnsEnterpriseDto){
        ReturnVO<JgPageVo<List<JSONObject>>> returnVO = iEnterQueAnsApiFeign.selectEnterList(adminQueAnsEnterpriseDto);
        List<EnterpriseQueAnsVo> enterpriseQueAnsVoArrayList = new ArrayList<EnterpriseQueAnsVo>();
        List<JSONObject> list = returnVO.getData().getData();
        list.forEach(model->{
            EnterpriseQueAnsVo enterpriseQueAnsVo1 = model.toJavaObject(EnterpriseQueAnsVo.class);
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("enterprise_id",enterpriseQueAnsVo1.getId());
            int count = communityQuestionsAnswersListMapper.selectCount(queryWrapper);
            enterpriseQueAnsVo1.setPublishCount(count);
            enterpriseQueAnsVo1.setCreateTime(enterpriseQueAnsVo1.getCreateTime());
            enterpriseQueAnsVoArrayList.add(enterpriseQueAnsVo1);
        });
        returnVO.getData().setData(enterpriseQueAnsVoArrayList);
        return new ReturnVO("企业发布数据查询成功",returnVO);
    }


    @Resource
    private BannedMapper bannedMapper;
    public ReturnVO<QueAnsUserInfoVo> getUserInfo(Long userId){
        QueAnsUserInfoVo queAnsUserInfoVo = new QueAnsUserInfoVo();
        FeignWxUserDto feignWxUserDto = iEnterQueAnsApiFeign.selectFeignWxUserDtoByWxUserId(userId);
//        queAnsUserInfoVo = JSONObject.parseObject(data).toJavaObject(QueAnsUserInfoVo.class);
        BeanUtils.copyProperties(feignWxUserDto,queAnsUserInfoVo);
        //查询发布数量
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("publish_person_id",userId);
        List<QuestionsAnswersList> list = questionsAnswersListMapper.selectList(queryWrapper);
        BigDecimal bigDecimal = new BigDecimal("0.00");
        queAnsUserInfoVo.setPublishCount(list.size());
        for (int i=0;i<list.size();i++){
            bigDecimal=bigDecimal.add(list.get(i).getMoneyReward());
        }
        queAnsUserInfoVo.setPutMoneyRewardCount(bigDecimal);
        //被采纳的数量
        QueryWrapper qw = new QueryWrapper();
        qw.eq("accept_user_id",userId);
        List<QueAnsAcceptInfo> list1 = queAnsAcceptInfoMapper.selectList(qw);
        queAnsUserInfoVo.setAcceptCount(list1.size());
        BigDecimal bigDecimal1 = new BigDecimal("0.00");
        list1.forEach(model->{
            queAnsUserInfoVo.setGetMoneyRewardCount(bigDecimal1.add(model.getMoneyReward()));
        });
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("user_id",userId);
        List<Banned> list2 = bannedMapper.selectList(queryWrapper1);
        queAnsUserInfoVo.setWdBanned(0);
        queAnsUserInfoVo.setSqBanned(0);
        list2.forEach(model->{
            Integer moduleType = model.getModuleType();
            Integer bannedStatus = model.getBannedStatus();
            if(moduleType==0&&bannedStatus==1){
                queAnsUserInfoVo.setWdBanned(1);
            }
            if(moduleType==1&&bannedStatus==1){
                queAnsUserInfoVo.setSqBanned(1);
            }
        });
        return new ReturnVO("查询成功",queAnsUserInfoVo);
    }
}
